package com.avic.rong.fragment;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.BaseFragment;
import com.avic.rong.MainActivity;
import com.avic.rong.R;
import com.avic.rong.adapter.show.AchdemanAdapter;
import com.avic.rong.adapter.show.AchrecomAdapter;
import com.avic.rong.adapter.show.AchsupplyAdaptere;
import com.avic.rong.adapter.show.ShowBannerAdapter;
import com.avic.rong.bean.show_bean.AchieveBean;
import com.avic.rong.course.activity.CompanysActivity;
import com.avic.rong.fragment.showapi.AchieveApi;
import com.avic.rong.listener.RocketOnScrollListener;
import com.avic.rong.transform.activity.TransformListActivity;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.SkipUtils;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.library.banner.BannerLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.example.minato.minastore.utils.AppUtils;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;

/**
 * Created by TSJ-2018-0492 on 2019/9/18.
 */

public class FragmentAchievements extends BaseFragment implements OnRefreshListener {

    private Context mContext;
    private String mTitle = "成果转化";
    private AchieveApi achieveApi;
    private boolean isrefresh = false;
    private List<String> bannerList = new ArrayList<>();
    private AchsupplyAdaptere achsupplyAdaptere;
    private AchrecomAdapter achrecomAdapter;
    private AchdemanAdapter achdemanAdapter;
    private List<AchieveBean.DemandListBean> demandListBeansnew = new ArrayList<>();
    private List<AchieveBean.RecomSListBean> recomSListBeansnew = new ArrayList<>();
    private List<AchieveBean.SupplyListBean> supplyListBeans = new ArrayList<>();
    private View view;
    private BannerLayout achieveBanner;
    /**
     * 需求大厅
     */
    private TextView achieve_xq_house;
    /**
     * 供应大厅
     */
    private TextView achieve_gy_house;
    /**
     * 企业大厅
     */
    private TextView achieve_qy_house;
    private LinearLayout resultTransform;
    /**
     * 更多
     */
    private TextView achieve_tjxq_go;
    private ImageView achieve_xuqiu_sanjiao;
    private RecyclerView recommedXuqiuRec;
    /**
     * 更多
     */
    private TextView achieve_tjgy_go;
    private ImageView achieve_gongying_sanjiao;
    private RecyclerView recommedGongyingRec;
    /**
     * 更多
     */
    private TextView achieve_zxgy_go;
    private ImageView achieve_new_gong_sanjiao;
    private RecyclerView recommedNewXuqiuRec;
    public SmartRefreshLayout achieveRefresh;
    private LoadingLayout achieveLoading;
    public NestedScrollView nestedScrollView;


    public static FragmentAchievements getInstance(String title, Context context) {
        FragmentAchievements sf = new FragmentAchievements();
        sf.mContext = context;
        sf.mTitle = title;
        return sf;
    }


    @Override
    protected int getContentViewId() {
        return R.layout.fragment_achievements;
    }

    @Override
    protected void initView() {
        achieveBanner = (BannerLayout) fragmentView.findViewById(R.id.achieve_banner);
        achieve_xq_house = (TextView) fragmentView.findViewById(R.id.achieve_xq_house);
        achieve_gy_house = (TextView) fragmentView.findViewById(R.id.achieve_gy_house);
        achieve_qy_house = (TextView) fragmentView.findViewById(R.id.achieve_qy_house);
        resultTransform = (LinearLayout) fragmentView.findViewById(R.id.resultTransform);
        achieve_tjxq_go = (TextView) fragmentView.findViewById(R.id.achieve_tjxq_go);
        achieve_xuqiu_sanjiao = (ImageView) fragmentView.findViewById(R.id.achieve_xuqiu_sanjiao);
        recommedXuqiuRec = (RecyclerView) fragmentView.findViewById(R.id.recommed_xuqiu_rec);
        achieve_tjgy_go = (TextView) fragmentView.findViewById(R.id.achieve_tjgy_go);
        achieve_gongying_sanjiao = (ImageView) fragmentView.findViewById(R.id.achieve_gongying_sanjiao);
        recommedGongyingRec = (RecyclerView) fragmentView.findViewById(R.id.recommed_gongying_rec);
        achieve_zxgy_go = (TextView) fragmentView.findViewById(R.id.achieve_zxgy_go);
        achieve_new_gong_sanjiao = (ImageView) fragmentView.findViewById(R.id.achieve_new_gong_sanjiao);
        recommedNewXuqiuRec = (RecyclerView) fragmentView.findViewById(R.id.recommed_new_xuqiu_rec);
        achieveRefresh = (SmartRefreshLayout) fragmentView.findViewById(R.id.achieve_refresh);
        achieveLoading = (LoadingLayout) fragmentView.findViewById(R.id.achieve_loading);
        achdemanAdapter = new AchdemanAdapter(getActivity());
        recommedXuqiuRec.setAdapter(achdemanAdapter);
        recommedXuqiuRec.setLayoutManager(new LinearLayoutManager(getActivity()));


        achrecomAdapter = new AchrecomAdapter(getActivity());
        recommedGongyingRec.setAdapter(achrecomAdapter);
        recommedGongyingRec.addItemDecoration(new GridSpacingItemDecoration(2, DensityUtils.dip2px(getActivity(), 10), false));
        recommedGongyingRec.setLayoutManager(new GridLayoutManager(getActivity(), 2));

        achsupplyAdaptere = new AchsupplyAdaptere(getActivity());
        recommedNewXuqiuRec.setAdapter(achsupplyAdaptere);
        recommedNewXuqiuRec.setLayoutManager(new LinearLayoutManager(getActivity()));

        achieveRefresh.setOnRefreshListener(this);
        achieveApi = new AchieveApi((RxAppCompatActivity) getActivity(), AchievehttpOnNextListener);
        achieveApi.setCache(false);
        achieveLoading.setStatus(LoadingLayout.LOADING);
        HttpManager.getInstance().doHttpDeal(achieveApi);
        achieveLoading.hideReturnButton();
        //监听滚动
        nestedScrollView = fragmentView.findViewById(R.id.nest_scrollview);
        nestedScrollView.setOnScrollChangeListener(new RocketOnScrollListener(((MainActivity)getActivity())));

        achieveLoading.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                HttpManager.getInstance().doHttpDeal(achieveApi);
            }

            @Override
            public void onReturn(View v) {

            }
        });


    }

    @Override
    protected void addListener() {

    }

    HttpOnNextListener<BaseResultEntity> AchievehttpOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {

            if (!baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)) {
                achieveRefresh.finishRefresh();
                return;
            }
            achieveLoading.setStatus(LoadingLayout.SUCCESS);
            achieveRefresh.finishRefresh();
            AchieveBean achieveBean = (AchieveBean) baseResultEntity.getData();
            //banner
            List<AchieveBean.AdvertisementContentBean> advertisementContent = achieveBean.getAdvertisementContent();
            for (int i = 0; i < advertisementContent.size(); i++) {
                bannerList.add(advertisementContent.get(i).getContent());
            }

            ShowBannerAdapter showBannerAdapter = new ShowBannerAdapter(getContext(), bannerList);
            showBannerAdapter.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    Logger.i("------------" + "位置" + position + "  ��");
                    String bannerUrl = AppUtils.COURSE_IMAGE_URL + advertisementContent.get(position).getContent();
                    SkipUtils.startSkipDetailIntent(getContext(), advertisementContent.get(position).getUrl(), bannerUrl);
                }
            });
            achieveBanner.setAdapter(showBannerAdapter);
            achieveBanner.setAutoPlayDuration(5000);
            achieveBanner.setAutoPlaying(true);
            //�Ƽ�����
            List<AchieveBean.DemandListBean> demandList = achieveBean.getDemandList();
            demandListBeansnew.addAll(demandList);
            if (demandListBeansnew.size() <= 4) {
                achdemanAdapter.setRecomSListBeans(demandListBeansnew);
            } else {
                while (demandListBeansnew.size() != 4) {
                    demandListBeansnew.remove(demandListBeansnew.size() - 1);
                }
                achdemanAdapter.setRecomSListBeans(demandListBeansnew);
            }
            //�Ƽ���Ӧ
            List<AchieveBean.RecomSListBean> recomSList = achieveBean.getRecomSList();
            recomSListBeansnew.addAll(recomSList);
            if (recomSListBeansnew.size() <= 4) {
                achrecomAdapter.setRecomSListBeans(recomSListBeansnew);
            } else {
                while (recomSListBeansnew.size() != 4) {
                    recomSListBeansnew.remove(recomSListBeansnew.size() - 1);
                }
                achrecomAdapter.setRecomSListBeans(recomSListBeansnew);
            }
            //���¹�Ӧ
            List<AchieveBean.SupplyListBean> supplyList = achieveBean.getSupplyList();
            supplyListBeans.addAll(supplyList);
            if (supplyListBeans.size() <= 6) {
                achsupplyAdaptere.setSupplyListBeans(supplyListBeans);
            } else {
                while (supplyListBeans.size() != 6) {
                    supplyListBeans.remove(supplyListBeans.size() - 1);
                }
                achsupplyAdaptere.setSupplyListBeans(supplyListBeans);
            }
            ;
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
        }
    };


    @OnClick({R.id.achieve_xq_house, R.id.achieve_gy_house, R.id.achieve_qy_house, R.id.achieve_tjxq_go, R.id.achieve_tjgy_go, R.id.achieve_zxgy_go})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.achieve_xq_house:
                startActivity(TransformListActivity.getIntent(getContext(), 0));
                break;
            case R.id.achieve_gy_house:
                startActivity(TransformListActivity.getIntent(getContext(), 1));
                break;
            case R.id.achieve_qy_house:
                mContext.startActivity(CompanysActivity.getIntent(mContext, CompanysActivity.class));
                break;
            case R.id.achieve_tjxq_go:
                break;
            case R.id.achieve_tjgy_go:
                break;
            case R.id.achieve_zxgy_go:
                break;
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isrefresh = true;
        clearlist();
        HttpManager.getInstance().doHttpDeal(achieveApi);


    }

    private void clearlist() {
        bannerList.clear();
        demandListBeansnew.clear();
        recomSListBeansnew.clear();
        supplyListBeans.clear();
    }


}
